var db = require('../../database')

module.exports = function (app, express) {
    var rout = express.Router();
    rout.post('/list', function (req, res){
        var tj = [], stj = '';
        var list = {
            status: 1,
            msg: '',
            data:{
                maxpage:0,
                list:[]
            }
        }
        /*if(!req.session['uid']){
            list.status = 401;
            list.msg = '未登录账号'
            list.data = {}
            res.json(list);
            return false;
        }*/
        var isRightNum = /^[0-9]*[1-9][0-9]*$/;
        var page = req.param('page');
        var limit= req.param('limit');
        if(page && limit && isRightNum.test(page) && isRightNum.test(limit)){
            var start = (page-1)*limit;
            var end = page*limit;
            stj = 'where status = 1 limit '+start+' , '+limit
            tj = 1
        }else{
            list.status = 0
            list.msg = '提交数据有误'
            list.data = {}
        }
        db.query("select * from user "+stj,function (err,rows) {//"select * from 数据表
            console.log(rows);
            if(err){
                list.status = 0
                list.msg = '提交数据有误'
                list.data = {}
            }else{
                list.status = 1
                list.msg = '查询成功'
                // list.data.maxpage = Math.ceil(rows.length/limit);
                // if(end>rows.length)end = rows.length;
                // if(start<0)start =0;
                for(var i=0;i<rows.length;i++){
                    var arr = {
                        'id':rows[i].id,
                        'username':rows[i].username
                    }
                    list.data.list.push(arr)
                }
                db.query("select count(*) from user where status=?;",tj,function (err1,rows1){
                    if(err1){
                        list.status = 0
                        list.msg = '提交数据有误2'
                        list.data = {}
                    }else{
                        list.data.maxpage = Math.ceil(rows1[0]['count(*)']/limit);
                        // console.log(rows1)
                    }
                    res.json(list);
                })
            }
        })
    })

    rout.post('/del', function(req,res){
        var tj = [], stj = '';
        var list = {
            status: 1,
            msg: '',
        }
        /*if(!req.session['uid']){
            list.status = 401;
            list.msg = '未登录账号'
            res.json(list);
            return false;
        }*/
        if(req.param('id')){
            stj = 'status = 0 where id = ?'//update 数据表 set字段名=字段值 where 条件表达式
            tj = req.param('id')
        }
        db.query('update user set '+stj,tj,function(err,rows){
            console.log(rows)
            if(err){
                list.status = 0
                list.msg = '提交数据有误'
            }else{
                if(rows.changedRows=='1'){
                    list.status = 1;
                    list.msg = '删除成功'
                }
            }
            res.json(list);
        })
    })
    return rout;
}

